﻿﻿package {
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DMovie;
	import _Pan3D.display3D.Display3DNormalSprite;
	import _Pan3D.display3D.Display3DShadowSprite;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.program.shaders.NormalShader;
	import _Pan3D.program.shaders.ScanningShader;
	import _Pan3D.program.shaders.ScanningShadow;
	import _Pan3D.program.shaders.ShadowShader;
	import _Pan3D.program.shaders.StatShader;
	import _Pan3D.texture.TextureManager;
	import _Pan3D.utils.Scanning;
	
	import _me.Config;
	import _me.KeyControl;
	import _me.MathCoreWillDele;
	import _me.Scene_data;
	import _me._ground.MakeGroundInfo;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	import com.bit101.components.FPSMeter;
	import com.bit101.components.HSlider;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	
	import flash.display.*;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	[SWF(backgroundColor="#FFFFFF",width="1024", height="600", frameRate="60")]
	public class indexPan extends MovieClip {
		private var keyControl : KeyControl = new KeyControl();
		private var projMatrix : PerspectiveMatrix3D;
		private var program : Program3D;
		private var program2 : Program3D;
		private var program3 : Program3D;
		private var program4 : Program3D;
		
		private var guaiwu : Display3DMovie;
		private var container : Display3DContainer;
		private var ground:Display3DSprite;
		private var wow:Display3DContainer;
		private var jianzhu:Display3DContainer;
		private var makeGroundInfo:MakeGroundInfo;
		
		public function indexPan() : void {
			Scene_data.cam3D.distance = 200;
			Scene_data.focus3D.angle_x = -30;
			addLines();
			addEvents();
			addView();
			
			
			
		}
		
		private function addLines() : void {
			var lines : Sprite = new Sprite();
			lines.graphics.clear();
			lines.graphics.lineStyle(1, 158485);
			lines.graphics.moveTo(stage.stageWidth / 2, 0);
			lines.graphics.lineTo(stage.stageWidth / 2, stage.stageHeight);
			lines.graphics.moveTo(0, stage.stageHeight / 2);
			lines.graphics.lineTo(stage.stageWidth, stage.stageHeight / 2);
			addChild(lines);
		}
		
		private function addEvents() : void {
			this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
		}
		
		private function addToStage(event : Event) : void {
			Config.init(stage, _config_end);
			keyControl.init(stage);
			keyControl.addEventListener(KeyboardEvent.KEY_DOWN,roleGo);
			keyControl.addEventListener(KeyboardEvent.KEY_UP,roleStop);
		}
		
		private function _config_end() : void {
			var context : Context3D = Scene_data.context3D;
			
			var nn : int = 1024;
			var tempBitmapData : BitmapData = new BitmapData(nn, nn, true, 0xFFFF0000);
			Scene_data.texShadowMap = context.createTexture(nn, nn, Context3DTextureFormat.BGRA, true);
			Scene_data.texShadowMap.uploadFromBitmapData(tempBitmapData, 0);
			
			Scene_data._sunMatrix.identity();
			Scene_data._sunMatrix.prependTranslation(0, 0, -4000);
			Scene_data._sunMatrix.prependRotation(120, new Vector3D(1, 0, 0));
			//Scene_data._sunMatrix.prependRotation(30, new Vector3D(0, 1, 0));
			Scene_data._sunMatrix.prependTranslation(0, 0, 0);
			
			Program3DManager.getInstance().initReg();
			
			program = Program3DManager.getInstance().getProgram(Md5Shader.MD5SHADER);
			program2 = Program3DManager.getInstance().getProgram(StatShader.STATSHADER);
			program3 = Program3DManager.getInstance().getProgram(NormalShader.NORMALSHADER);
			program4 = Program3DManager.getInstance().getProgram(ShadowShader.SHADOWSHADER);
			
			//program = context.createProgram();
			//program.upload(Scene_data.shaderDictionary["Md5"][0], Scene_data.shaderDictionary["Md5"][1]);
			//program2 = context.createProgram();
			//program2.upload(Scene_data.shaderDictionary["Stat"][0], Scene_data.shaderDictionary["Stat"][1]);
			makeScene();
		}
		
		private var meshAry : Array = ["basic_body", "basic_boots", "basic_glove", "basic_leg", "wuqi", "Hair01_Yellow", "Head01", "Helmet02_0"];
		
		private function makeScene() : void {
			var context : Context3D = Scene_data.context3D;
			TextureManager.getInstance().init(context);
			
			guaiwu = new Display3DMovie(context);
			guaiwu.addMesh("0", "default");
			//guaiwu.addMesh("1", "basic_body");
			guaiwu.addAnim("run", "run");
			guaiwu.addAnim("default", "default");
			guaiwu.setProgram3D(program);
			guaiwu.scale=0.2;
			
			//ground = new Display3DSprite(context);
			//ground.url = "box";
			//ground.setProgram3D(program2);
			//ground.scale = 10;
			//ground.y = 0;
			//ground.scale=50;
			wow = new Display3DContainer();
			//wow.addChild(ground);
			//wow.setProgram3D(program2);
			for(var i:int=0;i<66;i++){
				var sp:Display3DSprite = new Display3DShadowSprite(context);
				sp.url = i + "ground";
				sp.scale = 10;
				//sp.y=400;
				wow.addChild(sp);
				sp.addEventListener(Event.COMPLETE,ongroundCom);
			}
			wow.setProgram3D(program4);
			
			jianzhu = new Display3DContainer();
			for(i=0;i<224;i++){
				sp = new Display3DNormalSprite(context);
				sp.url = i + "jianzhu";
				sp.scale = 10;
				//sp.y=400;
				sp.addEventListener(Event.COMPLETE,onCom);
				jianzhu.addChild(sp);
			}
			
			jianzhu.setProgram3D(program3);
			
			container = new Display3DContainer();
			container.addChild(guaiwu);
			container.addChild(wow);
			container.addChild(jianzhu);
			
			this.addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		
		private var sp2 : Display3DSprite;
		private var objOcntainer : Display3DContainer;
		private var objOcntainer2 : Display3DContainer;
		
		private function enterFrame(event : Event) : void {
			// Scene_data.focus3D.angle_x=-45;
			
			keyControl.upData();
			MathCoreWillDele._catch_hit_cam(Scene_data.cam3D, Scene_data.focus3D);
			
			Scene_data.focus3D.angle_x = -10-(mouseY-300)/10;
			//	Scene_data.focus3D.angle_x =-90
			guaiwu.x=Scene_data.focus3D.x;
			guaiwu.y=-Scene_data.focus3D.z;
			guaiwu.z=Scene_data.focus3D.y-10;
			guaiwu.rotationY=Scene_data.focus3D.angle_y+180;
			
			if (!Scene_data.ready ) return;
			var context : Context3D = Scene_data.context3D;
			context.clear(0, 0, 0, 1);
			projMatrix = new PerspectiveMatrix3D();
			projMatrix.perspectiveFieldOfViewRH(1, 1, 0.1, 90000);
			
			context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 5, Vector.<Number>([100, 1, 0.5, 0]));
			context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, projMatrix, true);
			container.update();
			context.present();
			numScanNum++;
			if(numScanNum>200){
				scannHandle();
				numScanNum=0;
			}
		}
		private var numScanNum:int=0;
		
		private var loadingLabel:TextField;
		private function addView():void{
			makeGroundInfo=new MakeGroundInfo();
			makeGroundInfo.scaleX=makeGroundInfo.scaleY=0.3;
			makeGroundInfo.x=1024-makeGroundInfo.width;
			addChild(makeGroundInfo);
			//var btn:PushButton = new PushButton(stage,0,0,"scanning",scannHandle);
			
			new FPSMeter(this);
			var r:HSlider = new HSlider(this,80,0,rHandler);
			var g:HSlider = new HSlider(this,80,20,gHandler);
			var b:HSlider = new HSlider(this,80,40,bHandler);
			r.maximum = 1;
			r.minimum = -1;
			r.value = 0;
			g.maximum = 1;
			g.minimum = -1;
			g.value = 0;
			b.maximum = 1;
			b.minimum = -1;
			b.value = 0;
			new Label(this,0,580,"verson:2");
			loadingLabel = new TextField();
			loadingLabel.width = 200;
			loadingLabel.height = 400;
			loadingLabel.selectable = false;
			var textformat:TextFormat = new TextFormat();
			textformat.color = 0xffffff;
			textformat.size = 12;
			loadingLabel.defaultTextFormat = textformat;
			loadingLabel.x = 200;
			this.addChild(loadingLabel);
		}
		private function scannHandle(event:Event=null):void{
			if(scanning == null)
				scanning = new Scanning();
			var p:Program3D = Program3DManager.getInstance().getProgram(ScanningShader.SCANNINGSHADER);
			var bitmapdata:BitmapData = scanning.scan(wow,p);
			Scene_data.groundBitmapData=bitmapdata;
			makeGroundInfo.setBitmapData(Scene_data.groundBitmapData);
			//this.addChild(new Bitmap(bitmapdata));
			if(bitmap && bitmap.parent){
				bitmap.parent.removeChild(bitmap);
			}
			bitmap = new Bitmap(bitmapdata);
			bitmap.scaleX = bitmap.scaleY = 0.5;
			bitmap.x = 0
			bitmap.y = 0;
			//stage.addChildAt(bitmap,0);
		}
		private var scanning:Scanning;
		private var bitmap:Bitmap;
		private var isHasShadow:Boolean;
		private function scannShow(event:Event=null):void{
			//if(isHasShadow)
				//return;
			var p:Program3D = Program3DManager.getInstance().getProgram(ScanningShadow.SCANNINGSHADOW);
			if(scanning == null)
				scanning = new Scanning();
			var bitmapdata:BitmapData = scanning.scanShadow(jianzhu,p);
			if(bitmap && bitmap.parent){
				bitmap.parent.removeChild(bitmap);
			}
			bitmap = new Bitmap(bitmapdata);
			bitmap.scaleX = bitmap.scaleY = 0.2;
			bitmap.x = 0
			bitmap.y = 0;
			//stage.addChildAt(bitmap,0);
			//isHasShadow = true;
		}
		private function rHandler(event:Event):void{
			Scene_data.radd = HSlider(event.target).value;
		}
		private function gHandler(event:Event):void{
			Scene_data.gadd = HSlider(event.target).value;
		}
		private function bHandler(event:Event):void{
			Scene_data.badd = HSlider(event.target).value;
		}
		private function roleGo(event:Event):void{
			guaiwu.play("run");
		}
		private function roleStop(event:Event):void{
			guaiwu.play("default");
		}
		private var comNum:int;
		private function onCom(event:Event):void{
			comNum++;
			if(comNum==217){
				scannShow();
			}
			//trace("comNum *******" + comNum)
			loadingLabel.text = "地形：" + groundNum + "/66 \n" + "建筑：" + comNum + "/217";
		}
		private var groundNum:int;
		private function ongroundCom(event:Event):void{
			groundNum++;
			loadingLabel.text = "地形：" + groundNum + "/66 \n" + "建筑：" + comNum + "/218";
		}
		
	}
}

